package com.wust.data;

import java.util.ArrayList;
import java.util.List;

import com.wust.database.DatabaseFactory;
import com.wust.util.ConstDefine;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataDao {
	private Context context;
	private List<Data>datalist;
	public DataDao(Context context)
	{
		this.context=context;
	}
	public List<Data> queryByCatId(String catId)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.EXIST_DATABASE);
		SQLiteDatabase db=database.getReadableDatabase();
		datalist=new ArrayList<Data>();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.DATA_TABLE, null, "catid=?", new String[]{catId}, null, null, null);
		while(cursor.moveToNext())
		{
			Data data=new Data();
			data.setId(cursor.getInt(cursor.getColumnIndex("id")));
			data.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			data.setUid(cursor.getInt(cursor.getColumnIndex("uid")));
			data.setContent(cursor.getString(cursor.getColumnIndex("content")));
			data.setStatus(cursor.getString(cursor.getColumnIndex("status")));
			data.setCreate_time(cursor.getString(cursor.getColumnIndex("create_time")));
			data.setType_id(cursor.getString(cursor.getColumnIndex("type_id")));
			data.setGender(cursor.getString(cursor.getColumnIndex("gender")));
			data.setSmall_imgurl(cursor.getString(cursor.getColumnIndex("small_imgurl")));
			data.setMiddle_imgurl(cursor.getString(cursor.getColumnIndex("middle_imgurl")));
			data.setCatid(cursor.getInt(cursor.getColumnIndex("catid")));
			data.setS_catid(cursor.getInt(cursor.getColumnIndex("s_catid")));
			data.setVer(cursor.getString(cursor.getColumnIndex("ver")));
			data.setUpdate_time(cursor.getString(cursor.getColumnIndex("update_time")));
			datalist.add(data);
		}
		db.close();
		return datalist;
	}
	public Data getDataId(String Id)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.EXIST_DATABASE);
		SQLiteDatabase db=database.getReadableDatabase();
		Data data=new Data();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.DATA_TABLE, null, "id=?", new String[]{Id}, null, null, null);
		while(cursor.moveToNext())
		{
			
			data.setId(cursor.getInt(cursor.getColumnIndex("id")));
			data.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			data.setUid(cursor.getInt(cursor.getColumnIndex("uid")));
			data.setContent(cursor.getString(cursor.getColumnIndex("content")));
			data.setStatus(cursor.getString(cursor.getColumnIndex("status")));
			data.setCreate_time(cursor.getString(cursor.getColumnIndex("create_time")));
			data.setType_id(cursor.getString(cursor.getColumnIndex("type_id")));
			data.setGender(cursor.getString(cursor.getColumnIndex("gender")));
			data.setSmall_imgurl(cursor.getString(cursor.getColumnIndex("small_imgurl")));
			data.setMiddle_imgurl(cursor.getString(cursor.getColumnIndex("middle_imgurl")));
			data.setCatid(cursor.getInt(cursor.getColumnIndex("catid")));
			data.setS_catid(cursor.getInt(cursor.getColumnIndex("s_catid")));
			data.setVer(cursor.getString(cursor.getColumnIndex("ver")));
			data.setUpdate_time(cursor.getString(cursor.getColumnIndex("update_time")));
			
		}
		db.close();
		return data;
	}
}
